import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {Vehicle} from "../../../shared/model/vehicle";
import {CamManageService} from "../../../shared/service/cam-manage.service";
import {Router} from "@angular/router";

@Component({
  selector: 'app-cam-apply-list',
  templateUrl: './cam-apply-list.component.html',
  styleUrls: ['./cam-apply-list.component.css']
})
export class CamApplyListComponent implements OnInit {

  vehicles:Vehicle[] = [];
  oldVehicles: Vehicle[];
  selectedVehicle: Vehicle;
  @Input()
  vehicleoid: string;

  @Output()
  firstVehicle:EventEmitter<Vehicle> = new EventEmitter();

  @Output()
  vehicle:EventEmitter<Vehicle> = new EventEmitter();

  constructor(
    private camManageService:CamManageService
  ) { }

  ngOnInit() {
    this.getVehicles();


    //筛选符合搜索框中条件的RoomList集合
    this.camManageService.searchEvent.subscribe(param =>{
      let result = this.oldVehicles;
      if(param){
        result = result.filter((r:Vehicle)=>r.name.indexOf(param)!=-1);
      }
      this.vehicles = result;
    });

    this.camManageService.listFreshEvent.subscribe(
      param => {
        if (param) {
          this.getVehicles();
        }
      }
    );
  }

  getVehicles(){
    this.camManageService.getVehicle().then(
      res => {
        this.vehicles = res;
        this.oldVehicles = res;
        this.firstVehicle.emit(this.vehicles[0]);
        if(!this.vehicle){
          this.vehicleoid = this.vehicles[0].oid;
        }
      }
    );
  }

  onSelect(vehicle:Vehicle){
    this.vehicleoid = vehicle.oid;
    this.vehicle.emit(vehicle);
  }

}

